package com.woniu.register.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.common.util.ResultVO;
import com.woniu.register.VO.FileVo;
import com.woniu.register.dto.DirDTO;
import com.woniu.register.dto.FileDTO;
import com.woniu.register.service.PigeonholeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author ：zhulongxing
 * @date ：Created in 2022/12/8 15:31
 * @description：
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping("files")
public class FileController {
    @Autowired
    private PigeonholeService pigeonholeService;

    @PostMapping("selectAll")
    public ResultVO selectAll(@RequestBody FileDTO fileDTO){
        if(fileDTO.getBusinessNum()!=null){
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date str = sdf.parse(fileDTO.getBusinessNum());
                fileDTO.setBusinessNum(str.getTime()/1000+"");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List<FileVo> filelist = pigeonholeService.selectAll(fileDTO);
        PageInfo<FileVo> lists = new PageInfo<>(filelist);
        for (FileVo fileVo : filelist) {
            Date date = new Date();
            date.setTime(Long.parseLong(fileVo.getBusinessNum()));
            fileVo.setBusinessNum(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
        }
        return ResultVO.success("查询成功",lists);
    }

    @PostMapping("selectAlls")
    public ResultVO selectAlls(@RequestBody FileDTO fileDTO){
        if(fileDTO.getBusinessNum()!=null){
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date str = sdf.parse(fileDTO.getBusinessNum());
                fileDTO.setBusinessNum(str.getTime()/1000+"");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List<FileVo> filelist = pigeonholeService.selectAllS(fileDTO);
        PageInfo<FileVo> lists = new PageInfo<>(filelist);
        for (FileVo fileVo : filelist) {
            Date date = new Date();
            date.setTime(Long.parseLong(fileVo.getBusinessNum()));
            fileVo.setBusinessNum(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
        }
        return ResultVO.success("查询成功",lists);
    }
    @PutMapping("updateAndInsert")
    public ResultVO updateAndInsert(@RequestBody DirDTO dto){
        pigeonholeService.updateAndInsert(dto);
        return ResultVO.success("操作成功");
    }
}
